home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
drdobbs
/
1991
/
12
/
book.asc
next >
Wrap
Text File
|
1991-11-11
|
1KB
|
58 lines
BOOKSHELF.ASC
Title: PROGRAMMER'S BOOKSHELF
Source code that accompanies Andrew Schulman's book review of
"The Standard C Library" (Plauger) and "The C++ Programming
Language, Second Edition" (Stroustrup).
Example 1: A stack template in C++ 3.0
// stack.h
template<class T> class stack
{
T *v, *p;
int sz;
public:
// all the following functions are inline
stack(int s) { v = p = new T[sz = s]; }
~stack() { delete[] v; }
void push(T a) { *p++ = a; }
T pop() { return *--p; }
int size() const { return p-v; }
}
Example 2: Creating two different classes of stack, from the same
template
#include <iostream.h>
#include "stack.h"
main()
{
stack<int> si(100); // stack of 100 ints
stack<char> sc(100); // stack of 100 chars
for (int i=0; i<10; i++)
{
si.push(i);
sc.push(i);
}
while (si.size()) // pop everything and
cout << si.pop() << ' ' ; // display it
cout << '\n' ;
}
Example 3
mov bx, word ptr [si.p]
mov ax, word ptr [i]
mov word ptr [bx], ax ; *p = i
add word ptr [si.p], 2 ; p += sizeof(int)